package main

import (
	"fmt"
	"path"	
	"strings"

	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

//转换excel文件
func (e *editor)Trans() string {
	//读取excel中的内容	
	xlsxFile := readexec(e.uri)
	sheetname := xlsxFile.GetSheetName(0)
	colsNew ,err  := xlsxFile.GetCols(sheetname)
    col1 := colsNew[1]
	//col2 := colsNew[3]
	//col3:= colsNew[5]
	
	
    //写excel
	f := excelize.NewFile()
	index := f.NewSheet("target")
	for i:=1 ;i<len(col1);i++ {
		f.SetCellValue("target", fmt.Sprintf("A%d",i), col1[i])
		fmt.Printf(col1[i])    
	}
	f.SetActiveSheet(index)

	//获取文件名和路径，并指定转换后保存的文件名
	a := strings.Split(e.uri, "//")
	path, filename := path.Split(a[1])
	filenameNew := "transed_" + filename
    filepath := path + filenameNew
	fmt.Print(filepath)
    err = f.SaveAs(filepath)
    if err != nil {
        panic(err)
    }
   return filenameNew
}


//读取指定uri的文件
func readexec(fileUri string) *excelize.File {
	var a []string 
	if fileUri != "" {
		a = strings.Split(fileUri, "//")
		fmt.Println(a[1])		
	} else {
		fmt.Println("no select")		
	}
    xlsxFile ,err := excelize.OpenFile(a[1])
    if err != nil {
	    fmt.Println(err)		
    } 
    return xlsxFile
} 

